﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace ExcelDataDrivenTests.Tests
{
    [TestFixture]
    public class ExcelTestCaseDataReaderTests
    {

        [Test] public void New()
        {
            Assert.That(ExcelTestCaseDataReader.New(), Is.Not.Null);
        }

        [Test]
        public void ReadXlsFromFileSystemOneSheet()
        {
            var testcases = ExcelTestCaseDataReader.New()
                .FromFileSystem(@"E:\projects\oss\ExcelDataDrivenTests\ExcelDataDrivenTests.Tests\testcases-sample.xls")
                .AddSheet("Sheet1")
                .GetTestCases(delegate(string sheet, DataRow row, int rowNum)
                              {
                                  var testName = sheet + rowNum;
                                  var category = Convert.ToString(row["colA"]);
                                  IDictionary testDataArgs = new Hashtable();
                                  TestCaseData testData = new TestCaseData(testDataArgs).SetName(testName).SetCategory(category);
                                  return testData;
                              }
                 );
            Assert.That(testcases.Count, Is.EqualTo(4));
        }

        [Test]
        public void ReadXlsxFromFileSystemOneSheet()
        {
            var testcases = ExcelTestCaseDataReader.New()
                .FromFileSystem(@"E:\projects\oss\ExcelDataDrivenTests\ExcelDataDrivenTests.Tests\testcases-sample.xlsx")
                .AddSheet("Sheet1")
                .GetTestCases(delegate(string sheet, DataRow row, int rowNum)
                {
                    var testName = sheet + rowNum;
                    var category = Convert.ToString(row["colA"]);
                    IDictionary testDataArgs = new Hashtable();
                    TestCaseData testData = new TestCaseData(testDataArgs).SetName(testName).SetCategory(category);
                    return testData;
                }
                 );
            Assert.That(testcases.Count, Is.EqualTo(4));
        }

        [Test]
        public void ReadXlsFromEmbeddedResourceOneSheet()
        {

            var testcases = ExcelTestCaseDataReader.New()
                .FromEmbeddedResource(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, "testcases-sample.xls")
                .AddSheet("Sheet1")
                .GetTestCases(delegate(string sheet, DataRow row, int rowNum)
                {
                    var testName = sheet + rowNum;
                    var category = Convert.ToString(row["colA"]);
                    IDictionary testDataArgs = new Hashtable();
                    var testData = new TestCaseData(testDataArgs).SetName(testName).SetCategory(category);
                    return testData;
                }
            );
            Assert.That(testcases.Count, Is.EqualTo(4));
        }

        [Test]
        public void ReadXlsFromFileSystemAddingTwoSheets()
        {
            var testcases = ExcelTestCaseDataReader.New()
                .FromFileSystem(@"E:\projects\oss\ExcelDataDrivenTests\ExcelDataDrivenTests.Tests\testcases-sample.xls")
                .AddSheet("Sheet1")
                .AddSheet("Sheet2")
                .GetTestCases(delegate(string sheet, DataRow row, int rowNum)
                {
                    var testName = sheet + rowNum;
                    var category = Convert.ToString(row["colA"]);
                    var colB = Convert.ToString(row["colB"]);
                    IDictionary testDataArgs = new Hashtable();
                    TestCaseData testData = new TestCaseData(testDataArgs).SetName(testName).SetCategory(category);
                    return testData;
                }
                 );
            Assert.That(testcases.Count, Is.EqualTo(8));
        }

        [Test]
        [TestCaseSource("SampleTestCaseData")]
        public void New3(IDictionary testData)
        {
            
        }

        public static IEnumerable<TestCaseData> SampleTestCaseData
        {
            get
            {
                var testcases = ExcelTestCaseDataReader.New()
                    .FromFileSystem(@"E:\projects\oss\ExcelDataDrivenTests\ExcelDataDrivenTests.Tests\testcases-sample.xls")
                    .AddSheet("Sheet1")
                    .GetTestCases(delegate(string sheet, DataRow row, int rowNum)
                    {
                        var testName = sheet + rowNum;
                        //var category = Convert.ToString(row["col1"]);
                        IDictionary testDataArgs = new Hashtable();
                        TestCaseData testData = new TestCaseData(testDataArgs).SetName(testName);
                        return testData;
                    }
                 );

                foreach (TestCaseData testCaseData in testcases)
                {
                    yield return testCaseData;
                }
            }
        }

    }
}
